package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.Station;
import com.neusoft.my12306.entity.Ticket;
import com.neusoft.my12306.mapper.TicketMapper;
import com.neusoft.my12306.service.ITicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 葛彬
 * @since 2023-07-20
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements ITicketService {

    @Override
    public CommonResult querySeatnum(Ticket ticket, String fromSt, String toSt) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("trainno", ticket.getTrainno());
        queryWrapper.eq("date", ticket.getDate());
        queryWrapper.eq("carriage", ticket.getCarriage());
        queryWrapper.like("train_path", "%"+fromSt+"%");
        queryWrapper.like("train_path", "%"+toSt+"%");

        List<Station> lss = baseMapper.selectList(queryWrapper);
        CommonResult result = new CommonResult(lss);

        return result;
    }

    @Override
    public CommonResult querySeat(Ticket ticket) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("trainno", ticket.getTrainno());
        queryWrapper.eq("date", ticket.getDate());
        queryWrapper.eq("carriage", ticket.getCarriage());

        List<Ticket> records = baseMapper.selectList(queryWrapper);

        Ticket firstRecord = null;
        if (!records.isEmpty()) {
            firstRecord = records.get(0);
        }

        CommonResult result = new CommonResult(firstRecord);

        return result;
    }

    @Override
    public CommonResult insertSeat(Ticket ticket) {
        ticket.setTicketId(null);
        int i =  baseMapper.insert(ticket);
        CommonResult  result =new CommonResult( ticket.getTicketId());
        if (i <= 0 ){
            result.setNotModified();
        }

        return result;
    }

    @Override
    public CommonResult delSeat(Ticket ticket) {
        int i =  baseMapper.deleteById(ticket.getTicketId());
        CommonResult  result =new CommonResult(  i );
        if (i <= 0 ){
            result.setNotModified();
        }
        return result;
    }
}
